var nurseid="",perID=""
var TomcatUrl = ''
function UserInfo() {
	this.initData()
	this.initEvent()
}
UserInfo.prototype.initData = function() {
	mui.plusReady(function() {
		var imghead = mui("#imghead")[0]
		const loginInfo = app.getState();
		var name = loginInfo.loginName
		nurseid = loginInfo.loginID
		perID = loginInfo.loginCode
		TomcatUrl = loginInfo.TomcatUrl;
		if (name.length > 2) {
			name = name.substring(name.length-2,name.length)
		}
		imghead.setAttribute('data-name',name)
		var photo = loginInfo.PerPic
		if (photo && photo!="") {
			imghead.innerHTML = '<img src="'+photo +'" />'
			mui("#imghead img")[0].addEventListener('error',function(){
				var parentNode = this.parentNode
				parentNode.innerHTML = parentNode.getAttribute('data-name')
				parentNode.removeAttribute('style')
			})
		}else{
			imghead.innerHTML = name
			imghead.removeAttribute('style')
		}
		mui('#userCode')[0].innerText = loginInfo.loginCode;
		mui('#userName')[0].innerText = loginInfo.loginName;
		mui('#userLoc')[0].innerText = loginInfo.loginWardDesc;
	})
}


UserInfo.prototype.initEvent = function() {
	
	$click("qrcode",function(){
		$openWindow(this,'data-page')
	})
	mui("#imageInput")[0].addEventListener('change', function() {
		var file = this.files[0]
		getStatus(file)
	});
	
	function UploadFileToServer(file,filename){
		if (TomcatUrl == "") {
			mui.toast('TomcatUrl地址为空')
			return
		}
		plus.nativeUI.showWaiting("正在上传",{'modal':false})
		// FormData 对象
		var form = new FormData();
		form.append("file", file, filename);
		var xhr = null;
		if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		/*判断是否支持请求*/
		if (xhr == null) {
			mui.toast('你的浏览器不支持XMLHttp');
			return;
		}
		xhr.responseType = 'multipart/form-data';
		xhr.timeout = 12000;
		//为请求添加返回处理函数
		xhr.onreadystatechange = function() {
			if (this.readyState === 4 && this.status === 200) {
				//console.log(this.responseText)
				var result = this.responseText.split(':')[0];
				var resultDesc = this.responseText.split(':')[1];
				if (result == 'success') {
					mui.toast('上传成功');
					saveNursePic(resultDesc)
				} else {
					mui.toast(this.responseText);
					plus.nativeUI.closeWaiting();
				}
				
			} else {
			}
		};
		xhr.onerror = function(e) {
			plus.nativeUI.closeWaiting();
			mui.toast('上传响应错误');
		}
		xhr.onabort = function(e) {
			plus.nativeUI.closeWaiting();
			mui.toast('上传响应中断');
		}
		xhr.ontimeout = function(e) {
			plus.nativeUI.closeWaiting();
			mui.toast('上传响应超时');
		}
		xhr.open("post", TomcatUrl + '/Fileupdate/servlet/VueFileuploadServlet?filesname=images/head', true);
		xhr.send(form);
	}
	
	function getStatus(file){
		var pdict = {'id':nurseid} //获取档案审核状态
		var paramDict = {'method':'web.INMPersonComm:GetAuditStatus','params':JSON.stringify(pdict),'type':'Method'}
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				mui.toast(result.msg)
				return
			}
			if (result.msg != 'N') {
				mui.toast('只有未审核状态才能修改')
				return
			}
			var nameArr = file.name.split('.')
			var type = nameArr[nameArr.length - 1]
			if (type != "png" && type != 'jpg') {
				mui.toast('仅支持png和jpg格式')
				return
			}
			var filename = nurseid + '.' +type
			UploadFileToServer(file,filename)
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	
	function saveNursePic(url){
		var parr = 'RowID|'+nurseid+'^PerPic|'+url+'^PerID|'+perID+'^HISNO|'+'^PerCardId|'+'^Operator|'+nurseid
		var pdict = {'parr':parr,'type':'N'} //获取档案审核状态
		var paramDict = {'method':'web.INMPersonComm:Save','params':JSON.stringify(pdict),'type':'Method'}
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			plus.nativeUI.closeWaiting();
			if (result.msgcode != '1') {
				mui.toast(result.msg)
				return
			}
			mui.toast('保存成功')
		}, function(errorStr) {
			mui.toast(errorStr)
			plus.nativeUI.closeWaiting();
		});
	}
}

new UserInfo()

/*
PerID|3790^HISNO|3790^PerName|李洵^PerCardId|120101196710126908^PerSexDR|6||2^PerBirthday|1967-10-12
^PerAge|55^PerNation|1||1^PerPolitical|2||4^Marriage|^PerPhone|^Email|^PerClothesNo|^PerTrouserNo|8||3
^PerShoesNo|9||2^PerEspecially|^PerHeight|^NativeProvince|^NativeCity|^NativeArea|^NativeAddress|
^RegistedProvince|^RegistedCity|^RegistedArea|^RegistedAddress|^CurrentProvince|^CurrentCity|
^CurrentArea|^CurrentAddress|^HomeProvince|^HomeCity|^HomeArea|^HomeAddress|^Relationship|^PerContactPhone|
^PerContanct|^PerRemark|^PerRegNo|^PerRegRegiDate|2022-07-04^PerRegValidDate|2027-07-03^PerQualNo|
^PerForeignLevel|^PerInitialAcade|^PerInitialSchool|^PerInitialDegree|^PerCurrentAcade|^PerCurrentSchool|
^PerCurrentDegree|^PerSpeciNurse|^SelfAssessLevel|^PerNurseLevel|19||4^PerSocialWork|^PerWorkDate|1979-12-02^
PerDepDate|2003-01-02^PerComeDate|2003-01-02^PerNurTime|7419^PerLRetireDate|2022-09-28^PerSource|7||8^
PerDepDR|1^PerAttriDepart|27||1^PerHireDuty|12||1^PerPostDuty|^PerInNursePost|62||1^PerAllowance|^PerPostName|
^PerAuditFlag|N^AffirmLevel|^AuditReason|1^AuditTime|38272^ImportDate|2022-06-08^ImportUser|^InTrainCurrentAcade|
^PerAddress|^PerAssToNurse|^PerAssToStu|^PerAttriDepartDesc|职能部门^PerAuditDate|2023-01-11^PerAuditor|0^PerCadreDate|
^PerCareType|N^PerCategroy|^PerClothesNoDesc|^PerContact|^PerCurrentAcadeDesc|^PerCurrentDegreeDesc|^PerCurrentSchoolDesc|
^PerDepDRDesc|护理部^PerForeignLevelDesc|^PerHireDutyDate|2023-01-11^PerHireDutyDesc|护士^PerHireJob|^PerHobby|
^PerHomeAddress|^PerHosShortNo|^PerHouseAddress|^PerInNursePostDesc|定科^PerInitialAcadeDesc|^PerInitialDegreeDesc|
^PerInitialSchoolDesc|^PerInternDate|^PerInternEndDate|^PerInternSchool|^PerInternStDate|^PerInternToStu|^PerIsEspecially|
^PerIsRegiste|^PerIsSpeciNurse|^PerLocDR|^PerMarriage|^PerMarriageDesc|^PerNationDesc|汉族^PerNativePlace|^PerNo|
^PerNurType|^PerNurseLevelDesc|N3^PerOperate|409@%Library.RelationshipObject^PerPic|Fileupdate/images/head/65.jpg
^PerPolitDate|^PerPoliticalDesc|预备党员^PerPostCategory|^PerPostCategoryDesc|^PerPostCompete|^PerPostDutyDate|^
PerPostDutyDesc|^PerPostNameDesc|^PerPostType|^PerPostTypeDesc|^PerResignDate|^PerRetireDate|^PerSJHireDuty|^PerSJHireDutyDate|^PerSexDRDesc|女^PerShoesNoDesc|38^PerSocialWorkDesc|^PerSocietyPart|^PerSourceDesc|合同员工^PerStateDate|^PerStatuReason|^PerStatus|17||1^PerStuToAss|^PerStuToNurse|^PerTrainEndDate|^PerTrainFee|^PerTrainFlag|^PerTrainStDate|^PerTransFlag|^PerTrialDate|^PerTrialQual|^PerTrialWork|^PerTrouserNoDesc|S^PerType|^PerTypeDR|N^PerUnit|^PerUserDR|^PerWeight|^PerWordType|^PerYear|^PrepareDate|^RecDate|^RecTime|^RecUser|^RelationshipDesc|^RowID|65^Operator|1
*/
